﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomSecurity;
using GeneralLibs;
using BarCode;
using SOP;
using Lists.SOP;

namespace Portal
{
    public partial class UcWorkflowClinic1 : UserControl
    {
        public delegate void OnRequestLogoutHandler();
        public event OnRequestLogoutHandler RequestLogout;

        public UcWorkflowClinic1()
        {
            InitializeComponent();
        }

        public delegate void CallFunctionHandler(object caller, string function);
        public event CallFunctionHandler CallFunction;

        private void UcWorkflow_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            foreach (Control control in this.Controls)
            {
                SetEnableStateRecuirement(control);
            }

            string tenDoanhNghiepVietHoa = Config.TenDoanhNghiep.ToUpper();
        }

        private void SetEnableStateRecuirement(Control control)
        {
            foreach (Control childControl in control.Controls)
            {
                SetEnableStateRecuirement(childControl);
            }

            if (control.ContextMenuStrip != null)
            {
                foreach (ToolStripItem item in control.ContextMenuStrip.Items)
                {
                    if (item.Tag is string)
                    {
                        item.Enabled = CustomSecurity.User.CurUser.CheckPrivilege(item.Tag as string, CustomSecurity.Right.Read);
                    }
                }
            }
            else if (control.Tag is string)
            {
                control.Enabled = CustomSecurity.User.CurUser.CheckPrivilege(control.Tag as string, CustomSecurity.Right.Read);
            }

            btnDangXuat.Visible = true;
            btnDangXuat.Enabled = true;
        }

        protected void Call(string function)
        {
            if (CallFunction != null)
            {
                CallFunction(this, function);
            }
        }

        public virtual void ShowHideFunction()
        {
            if (User.CurUser == null)
            {
                this.Enabled = false;
                return;
            }
            else
            {
                this.Enabled = true;
                foreach (Control control in this.Controls)
                {
                    if (control is FlowLayoutPanel)
                    {
                        foreach (Control btnControl in (control as FlowLayoutPanel).Controls)
                        {
                            if (btnControl is Button && (btnControl.Tag is string))
                            {
                                Function function = FunctionList.GetByName(btnControl.Tag as String);
                                if (function == null)
                                    continue;
                                else
                                    btnControl.Visible = (function.PosibleRight & User.CurUser.GetRight(function.ID)) > 0;
                            }
                        }
                    }
                }
            }
        }

        private void UcWorkflowClinic1_Resize(object sender, EventArgs e)
        {
            try
            {
                //565 là độ dài , 455 là độ cao của picturebox
                int X_picturebox = ((sender as UcWorkflowClinic1).Width - 565) / 2;
                int Y_picturebox = ((sender as UcWorkflowClinic1).Height - 455) / 2;
                pictureBox1.Left = X_picturebox;
                pictureBox1.Top = Y_picturebox;

            }
            catch (Exception ex)
            {
            }
        }

        #region Các btn click
        private void btnDangKiKham_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnDangKiKham.Tag = "FrmDanhSachPhieuDangKyKham";
            }
            else
            {
                btnDangKiKham.Tag = "FrmEditPhieuDangKyKham";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnDvTheoYeuCau_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnDvTheoYeuCau.Tag = "FrmDanhSachPhieuChiDinhYeuCau";
            }
            else
            {
                btnDvTheoYeuCau.Tag = "FrmEditPhieuChiDinhYeuCau";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnDvBSChiDinh_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnDvBSChiDinh.Tag = "FrmDanhSachPhieuChiDinhBacSi";
            }
            else
            {
                btnDvBSChiDinh.Tag = "FrmEditPhieuChiDinhBacSi";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnHoSoKhamBenh_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnHoSoKhamBenh.Tag = "FrmDanhSachHoSoKhamBenh";
            }
            else
            {
                btnHoSoKhamBenh.Tag = "FrmEditHoSoKhamBenh";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnPhieuKQDichVu_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnPhieuKQDichVu.Tag = "FrmDanhSachPhieuQuanLyKetQuaDichVuCLS";
            }
            else
            {
                btnPhieuKQDichVu.Tag = "FrmEditPhieuQuanLyKetQuaDichVuCLS";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnNhapThuoc_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnNhapThuoc.Tag = "FrmDanhSachHoaDonMuaHang";
            }
            else
            {
                btnNhapThuoc.Tag = "FrmEditHoaDonMuaHang";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnXuatThuoc_Click(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Control)
            {
                btnXuatThuoc.Tag = "FrmDanhSachHoaDonBanLe";
            }
            else
            {
                btnXuatThuoc.Tag = "FrmEditHoaDonBanLe1366x768Clinic";
            }

            Call((sender as Button).Tag as string);
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            if (RequestLogout != null) RequestLogout();
        }

        #endregion

        private void bntBaoCaoDichVu_Click(object sender, EventArgs e)
        {
            if (User.CurUser.UserName.ToUpper() != "KT") //Nếu là tài khoản này thì không báo cáo thống kê gì hết
                contextMenuBaoCaoPhongKham.Show(bntBaoCaoDichVu, new Point(0, 0));
        }

        private void bntBaoCaoKhoHang_Click(object sender, EventArgs e)
        {
            if (User.CurUser.UserName.ToUpper() != "KT") //Nếu là tài khoản này thì không báo cáo thống kê gì hết
                contextMenuBaoCaoKho.Show(bntBaoCaoKhoHang, new Point(0, 0));
        }

        private void btnBaoCaoKeToan_Click(object sender, EventArgs e)
        {
            if (User.CurUser.UserName.ToUpper() != "KT") //Nếu là tài khoản này thì không báo cáo thống kê gì hết
                contextMenuKeToan.Show(btnBaoCaoKeToan, new Point(0, 0));
        }

        private void sổChiTiếtCôngNợKếtHợpPhảiThuPhảiTrảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void sổTổngHợpCôngNợKếtHợpPhảiThuPhảiTrảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void bảngĐánhGiáTìnhHìnhThanhToánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêChiTiếtTheoTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void báoCáoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void tồnKhoTheoHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngTồnToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêChiTiếtNhậpXuấtKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngChuyểnKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void thốngKêHàngĐãKiểmKêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void bảngKêThuTiềnKhámBệnhlàmThuếToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem10_Click_1(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }

        private void toolStripMenuItem11_Click_1(object sender, EventArgs e)
        {
            Call((sender as ToolStripDropDownItem).Tag as string);
        }
    }
}
